package org.s170.dao;

import org.s170.entity.NoteType;
import org.s170.utils.Dbutil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class NoteTypeDao {

    //查找所有文章类型
    public List<NoteType> getAllNoteType(){
        List<NoteType> list=new ArrayList<NoteType>();
        Connection conn=null;
        PreparedStatement psmt=null;
        ResultSet rs=null;
        NoteType noteType=null;
        try {
            String sql="select * from NoteType";
            conn=Dbutil.getConnection();
            psmt=conn.prepareStatement(sql);
            rs=psmt.executeQuery();
            while (rs.next()){
                noteType = new NoteType();
                noteType.setTid(rs.getString(1));
                noteType.setTypeName(rs.getString(2));
                list.add(noteType);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            Dbutil.closeAll(conn,psmt,rs);
        }
        return list;
    }

    //添加文章类型
    public int addNoteType(NoteType noteType){
        String sql="insert into noteType(tid,typename) values(?,?)";

        Object[] obj= {
            noteType.getTid(),
            noteType.getTypeName()
        };

        int row= Dbutil.executeUpdate(sql,obj);
        return row;
    }

    //修改
    public  int updateNoteType(NoteType noteType){
        String sql="update  noteType  set typename=?  where tid=?";

        Object[] obj={
                noteType.getTypeName(),
                noteType.getTid()
        };

        int row=Dbutil.executeUpdate(sql,obj);
        return row;
    }

    //删除
    public  int deleteNoteType(String tid){
        String sql="delete from noteType where tid=?";

        Object[] obj={
                tid
        };

        int row=Dbutil.executeUpdate(sql,obj);
        return row;
    }


    public static void main(String[] args) {
//        NoteTypeDao dao=new NoteTypeDao();
//        List<NoteType> list=dao.getAllNoteType();
//        for (NoteType noteType : list) {
//            System.out.println(noteType);
//        }
    }

}
